home *** CD-ROM | disk | FTP | other *** search
/ 220 Jogos / 220 jogos.iso / tetris / tetron / SOURCE / GAMEMAIN.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  2002-08-14  |  12.9 KB  |  456 lines

  1. //----------------------------------------------------------------------------------------
  2. //----------------------------------------------------------------------------------------
  3. //
  4. //        Filename        :    gamemain.cpp
  5. //        Description        :    Member Definitions GameMain class
  6. //        Author            :   Marnich van Rensburg (2002)
  7. //
  8. //----------------------------------------------------------------------------------------
  9. //----------------------------------------------------------------------------------------
  10.  
  11.  
  12. #include "gamemain.h"
  13.  
  14.  
  15. //----------------------------------------------------------------------------------------
  16. //        Description        :    Constructor
  17. //----------------------------------------------------------------------------------------
  18.  
  19. GameMain :: GameMain()
  20. {
  21.     //Initialize all variables and objects
  22.     ConCurrent.New();
  23.     TetCurrent.New(1, 1);
  24.     TetNext.New(1, 1);
  25.     Stats.Score = 0;
  26.     Stats.Level = 0;
  27.     Stats.Lines = 0;
  28.     Drop = 0;
  29.     Stats.TetTotal = 0;
  30.     
  31.     for (int n = 0; n < TET_MAX_TYPES; n++)
  32.         Stats.TetTypeCount[n++] = 0; 
  33.     
  34. }// GameMain
  35.  
  36.  
  37.  
  38. //----------------------------------------------------------------------------------------
  39. //        Description        :    Initialize the Game eg. sets up the game window
  40. //----------------------------------------------------------------------------------------
  41.  
  42. bool GameMain :: Init()
  43. {
  44.     GameGL.SetFullScreen(true);            // Fullscreen Mode
  45.  
  46.     // Create OpenGL Window    640 x 480 x 16
  47.     if (!GameGL.CreateGLWindow( "GAME_NAME", 640, 480, 16, GameGL.GetFullScreen()))
  48.         return false;                    // Quit if window was not created
  49.  
  50.     GameGL.InitGL();                    // Init game specific GL defaults
  51.  
  52.     HighScores.Init();
  53.     
  54.     srand( (unsigned)time( NULL ) );    // Seed for random number gen based on timer
  55.  
  56.     return true;                        // Initialization was OK
  57.  
  58. }// Init
  59.  
  60.  
  61.  
  62.  
  63. //----------------------------------------------------------------------------------------
  64. //        Description        :    Reset Game Vars ie. start a new game
  65. //----------------------------------------------------------------------------------------
  66.  
  67. void GameMain :: New()
  68. {
  69.     Mode = GAME_START_SCREEN;            //
  70.     GameOver = false;
  71.     TetCurrent.New(Random(), 1);        // Init a random Tetramino (Current "Active")
  72.     TetNext.New(Random(), 1);            // Init a random Tetramino (Next)
  73.  
  74.     ConCurrent.New();                    // Init a New Container
  75.     ConCurrent.PlaceTet(TetCurrent);    // Place the new tetramino onto the Container
  76.  
  77.     Speed = 1000;                        // Reset the falling speed
  78.     Drop = false;                        // No tetranimoes being droped
  79.     DropHeight = 0;                        // Not dropped ie. zero height
  80.     
  81.     Stats.Score = 0;                    // Reset the score
  82.     Stats.Level = 1;                    // Reset the level
  83.     Stats.Lines = 0;                    // Reset the Line couter
  84.     Stats.TetTotal = 0;                    // Zero tetraminoes used in play
  85.     for (int n = 0; n < TET_MAX_TYPES; n++)
  86.         Stats.TetTypeCount[n] = 0;        // Init individual tetramino type counters
  87.  
  88. }// New
  89.  
  90.  
  91.  
  92.  
  93. //----------------------------------------------------------------------------------------
  94. //        Description        :    Calculates new score based on defined values and object vars
  95. //----------------------------------------------------------------------------------------
  96.  
  97. void GameMain :: SetScore(char v_NoOfLines)
  98. {
  99.     int TmpVal = 0;
  100.  
  101.     switch(v_NoOfLines)        // How many lines removed with a tetramino
  102.     {
  103.         case 1    : { TmpVal = SCR_ONE_LINE_REMOVED; break;}
  104.         case 2    : { TmpVal = SCR_TWO_LINES_REMOVED; break;}
  105.         case 3    : { TmpVal = SCR_THREE_LINES_REMOVED; break;}
  106.         case 4    : { TmpVal = SCR_FOUR_LINES_REMOVED; break;}
  107.         default    :    TmpVal = 1;
  108.     }//switch
  109.  
  110.     TmpVal += Stats.Level *    SCR_TETRAMINO_DROPPED * TmpVal + DropHeight;    // Bonus points for dropping
  111.     Stats.Score += TmpVal;        // Adjust the current score
  112.     
  113.     DropHeight = 0;
  114.  
  115. }// SetScore
  116.  
  117.  
  118.  
  119. //----------------------------------------------------------------------------------------
  120. //        Description        :    Displays main startup screen
  121. //----------------------------------------------------------------------------------------
  122.  
  123. void GameMain :: StartScreen(unsigned char v_UserAction)
  124. {
  125.     if(v_UserAction == 32)
  126.         Mode = GAME_ACTIVE;
  127.  
  128.     if(v_UserAction == 'S')
  129.         StarFieldActive = !StarFieldActive;
  130.  
  131.     if(v_UserAction == 'M')
  132.         SoundActive = !SoundActive;
  133.  
  134.     GameGL.RenderStart();
  135.         GameGL.RenderStarField();
  136.         GameGL.RenderStartScreen(HighScores);
  137.     GameGL.RenderEnd();
  138.     
  139.     SwapBuffers(GameGL.GethDC());    // Swap Buffers (Double Buffering)
  140.  
  141. }//end StartScreen
  142.  
  143.  
  144.  
  145. //----------------------------------------------------------------------------------------
  146. //        Description        :    verifies user text input
  147. //----------------------------------------------------------------------------------------
  148.  
  149. bool GameMain :: ValidInput(unsigned char Val)
  150. {
  151.     static bool ValidStatus;
  152.     ValidStatus = false;
  153.     static char ValidChars[] = "0123456789abcdefghijklmonpqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  154.  
  155.     for(int n = 0; ValidChars[n] != '\0'; n++)
  156.         if(ValidChars[n] == Val) 
  157.             ValidStatus = true;
  158.  
  159.     return ValidStatus;
  160.         
  161. }//end ValidInput
  162.  
  163.  
  164. //----------------------------------------------------------------------------------------
  165. //        Description        :    Allows player to enter thier names
  166. //----------------------------------------------------------------------------------------
  167.  
  168. void GameMain :: GetHighScore(unsigned char v_UserAction)
  169. {
  170.     static char Name[25] = ".......................";
  171.     static char n = 0;
  172.  
  173.     //Enter
  174.     if(v_UserAction == 13)
  175.     {
  176.         Mode = GAME_START_SCREEN;
  177.         //Save the Score
  178.         HighScores.SetEntry(Name, Stats.Score, Stats.Lines, Stats.Level);
  179.     }//if
  180.  
  181.  
  182.     //Backspace
  183.     if(v_UserAction == 8)
  184.     {
  185.         if(n > 0)
  186.         {
  187.             Name[n - 1] = '.';
  188.             n--;
  189.         }//if
  190.     }//if
  191.  
  192.     if(v_UserAction == ' ')
  193.     {
  194.         Name[n] = '.';
  195.         n++;
  196.     }//if
  197.  
  198.     if((n < 23) && v_UserAction)
  199.     {
  200.         if(ValidInput(v_UserAction))
  201.         {
  202.             Name[n] = v_UserAction;
  203.             n++;
  204.         }
  205.     }//if
  206.  
  207.     if(n < 0) n = 0;
  208.  
  209.     GameGL.RenderStart();
  210.         GameGL.RenderStarField();
  211.         GameGL.RenderGameTitle(-1.0f, 1.5f);
  212.         GameGL.RenderHighScoreScreen(Name, n, Stats.Score);
  213.         //GameGL.DebugVal(v_UserAction);
  214.     GameGL.RenderEnd();
  215.     
  216.     SwapBuffers(GameGL.GethDC());    // Swap Buffers (Double Buffering)
  217.  
  218. }//end 
  219.  
  220.  
  221.  
  222.  
  223. //----------------------------------------------------------------------------------------
  224. //        Description        :    Main gameplay sequence ie. the Gameloop
  225. //----------------------------------------------------------------------------------------
  226.  
  227. void GameMain :: Play(unsigned char v_UserAction)
  228. {
  229.     if(GameOver) 
  230.     {
  231.         ConCurrent.SetAction(CON_GAME_OVER);
  232.         if(v_UserAction != 0)
  233.         {
  234.             if(HighScores.IsNewHighScore(Stats.Score))
  235.                 Mode = GAME_NEW_HIGHSCORE;
  236.             else
  237.                 Mode = GAME_START_SCREEN;
  238.         }//if
  239.         v_UserAction = 0;
  240.  
  241.     }//if
  242.     
  243.     //incase numlock is on 
  244.     if (v_UserAction == VK_NUMPAD4) v_UserAction = PLR_MOVE_LEFT;
  245.     if (v_UserAction == VK_NUMPAD6) v_UserAction = PLR_MOVE_RIGHT;
  246.     if (v_UserAction == VK_NUMPAD2) v_UserAction = PLR_MOVE_DOWN;
  247.     if (v_UserAction == VK_NUMPAD0) v_UserAction = PLR_MOVE_DROP;
  248.  
  249.     if(Drop || TetCurrent.DropToNextRow()) 
  250.         v_UserAction = PLR_MOVE_DOWN;                            // A tetramino is being dropped by user or timer
  251.  
  252.     if (ConCurrent.GetAction() == CON_NONE)                        // Use user input only if no actions are active
  253.     {
  254.         switch (v_UserAction)                                    // What user action was performed
  255.         {
  256.             case PLR_MOVE_LEFT:                                    // MOVE LEFT
  257.             {
  258.                 GameGL.RenderRotationArrows(v_UserAction);        // Show Left rotation arrow
  259.                 ConCurrent.RemoveTet(TetCurrent);                // Remove Tetramino from Container
  260.                 TetCurrent.MoveLeft();                            // Move Tetramino to the left
  261.                 if (ConCurrent.PlaceTet(TetCurrent))            // Place the Tetramino & Check for collision
  262.                     ConCurrent.SetAction(CON_ROTATE_RIGHT);        // Tell container to rotate to the right
  263.                 else{
  264.                     TetCurrent.MoveRight();                        // If Yes, Move Tetramino back to old position
  265.                     ConCurrent.PlaceTet(TetCurrent);            // Place the Tetramino
  266.                 }//if
  267.                 break;            
  268.             }//case
  269.             
  270.             case PLR_MOVE_RIGHT:                                // MOVE RIGHT
  271.             {
  272.                 GameGL.RenderRotationArrows(v_UserAction);        // Show Right rotation arrow
  273.                 ConCurrent.RemoveTet(TetCurrent);
  274.                 TetCurrent.MoveRight();
  275.                 if (ConCurrent.PlaceTet(TetCurrent))
  276.                     ConCurrent.SetAction(CON_ROTATE_LEFT);
  277.                 else{
  278.                     TetCurrent.MoveLeft();                
  279.                     ConCurrent.PlaceTet(TetCurrent);
  280.                 }//if
  281.                 break;            
  282.             }//case
  283.             
  284.             case PLR_MOVE_DOWN:                                    // MOVE DOWN
  285.             {
  286.                 ConCurrent.RemoveTet(TetCurrent);
  287.                 TetCurrent.MoveDown();
  288.                 if (!ConCurrent.PlaceTet(TetCurrent))
  289.                 {
  290.                     TetCurrent.MoveUp();                
  291.                     ConCurrent.PlaceTet(TetCurrent);
  292.                     if(SoundActive) PlaySound("data/blip.wav", NULL, SND_ASYNC);// Play sound
  293.                     Drop = 0;                                    // Is no longer being dropped
  294.                     ConCurrent.SetAction(CON_CHECK_FOR_LINES);    // Tell container to check for lines
  295.                 }//if
  296.                 break;            
  297.             }//case
  298.  
  299.             case PLR_MOVE_DROP:                                    // DROP
  300.             {
  301.                 Drop = true;                                    // Set drop state to active (true)
  302.                 DropStarty = TetCurrent.y;                        // 
  303.                 DropHeight = 17 - TetCurrent.y;                    // Calc Hieght of drop 
  304.                 break;            
  305.             }//case
  306.  
  307.             case PLR_ROTATE_RIGHT:                                // ROTATE RIGHT
  308.             {
  309.                 ConCurrent.RemoveTet(TetCurrent);
  310.                 TetCurrent.RotateRight();
  311.                 if (!ConCurrent.PlaceTet(TetCurrent))
  312.                 {
  313.                     TetCurrent.RotateLeft();                
  314.                     ConCurrent.PlaceTet(TetCurrent);
  315.                 }//if
  316.                 break;            
  317.             }//case        
  318.  
  319.             case PLR_ROTATE_LEFT:                                // ROTATE LEFT
  320.             {
  321.                 ConCurrent.RemoveTet(TetCurrent);
  322.                 TetCurrent.RotateLeft();
  323.                 if (!ConCurrent.PlaceTet(TetCurrent))
  324.                 {
  325.                     TetCurrent.RotateRight();                
  326.                     ConCurrent.PlaceTet(TetCurrent);
  327.                 }//if
  328.                 break;            
  329.             }//case
  330.  
  331.             case 'S':                                            // Turn starfield on/off
  332.             {
  333.                 StarFieldActive = !StarFieldActive;
  334.                 break;
  335.             }//case
  336.  
  337.             case 'M':                                            // Turn starfield on/off
  338.             {
  339.                 SoundActive = !SoundActive;
  340.                 break;
  341.             }//case
  342.  
  343.         }//switch
  344.     }//if
  345.  
  346.     ConCurrent.DoAction();                                    // Allow container to perform its current active actions
  347.     
  348.     if((ConCurrent.GetAction() == CON_START_NEXT) && !GameOver)    // If Containers current action is Start Next    
  349.     {
  350.         //Check for GameOver condition
  351.         if(TetCurrent.y <= 0)
  352.         {
  353.             GameOver = true;
  354.             GameGL.RenderGameOver("New");            
  355.         }//if
  356.  
  357.         ConCurrent.SetAction(CON_NONE);                        // Action performed, No more actions
  358.         SetScore(Stats.Lines += ConCurrent.GetNoOfLinesFound());    // Update the score and inc line total
  359.         ConCurrent.ResetNoOfLinesFound();                    // Reset current line count val ie. zero
  360.         
  361.         //Go to next level
  362.         if(Stats.Lines > (Stats.Level * 10))
  363.             Stats.Level++;
  364.  
  365.         if(!GameOver)
  366.             StartNext();                                        // Place the Next tetramino onto the container
  367.  
  368.     }//if
  369.  
  370.     // Render all game elements to screen
  371.     Render();
  372.  
  373. }// Play
  374.  
  375.  
  376.  
  377.  
  378.  
  379. //----------------------------------------------------------------------------------------
  380. //        Description        :    Sets NEXT piece to CURRENT and inits another NEXT piece
  381. //----------------------------------------------------------------------------------------
  382.  
  383. void GameMain :: StartNext()
  384. {
  385.     Stats.TetTypeCount[TetCurrent.Type - 1]++;    // Increment the appropriate tet count
  386.     Stats.TetTotal++;                            // Increment tatal for tetraminos
  387.     char tmp_x = TetCurrent.x;                    // Remebers prev x position in container
  388.     TetCurrent.New(TetNext.Type, Stats.Level);    // Sets the TetCurrent = TetNext
  389.     TetCurrent.x = tmp_x;                        // Position it to the current x pos
  390.     TetNext.New(Random(), Stats.Level);            // Create a new TetNext
  391.     ConCurrent.PlaceTet(TetCurrent);            // Place the new tetra in the container
  392.  
  393. }// StartNext
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401. //----------------------------------------------------------------------------------------
  402. //        Description        :    Render the game on the screen
  403. //----------------------------------------------------------------------------------------
  404.  
  405. void GameMain :: Render()
  406. {
  407.     GameGL.RenderStart();
  408.         GameGL.RenderStarField();
  409.         GameGL.RenderGameTitle(-5.0f, -4.0f);
  410.         GameGL.RenderGameStats(Stats);                // Score etc.
  411.         GameGL.RenderContainer(ConCurrent);            // Container 
  412.         GameGL.RenderNextPreview(TetNext);
  413.         GameGL.RenderRotationArrows(0);
  414.         GameGL.RenderDropArrow(DropStarty, TetCurrent.y, Drop);
  415.         if(GameOver) GameGL.RenderGameOver("");
  416.     GameGL.RenderEnd();
  417.     
  418.     SwapBuffers(GameGL.GethDC());    // Swap Buffers (Double Buffering)
  419.  
  420. }// Render
  421.  
  422.  
  423.  
  424.  
  425.  
  426. //----------------------------------------------------------------------------------------
  427. //        Description        :    Shutdown
  428. //----------------------------------------------------------------------------------------
  429.  
  430. void GameMain :: Quit()
  431. {
  432.  
  433.     GameGL.KillGLWindow();
  434.  
  435. }//End Show
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442. //----------------------------------------------------------------------------------------
  443. //        Description        :    For generating random tetraminos
  444. //----------------------------------------------------------------------------------------
  445.  
  446. char GameMain :: Random()
  447. {
  448.     double rn = 0;
  449.  
  450.     rn = ((double)rand() / (double)(RAND_MAX + 1)); // random float value in range [0,1) 
  451.                                      
  452.     rn = (char)(rn * TET_MAX_TYPES);
  453.  
  454.     return (char)rn + 1;
  455.  
  456. }//End Random